/**全局变量配置*/
var Global =
    {
        RET_SUCCESS: 0,                      //协议返回值成功的标识
    };

Global.init = function () {
    const fs = require("fs");
    const path = require('path');
    let config = fs.readFileSync(path.join(__dirname, './wdConfig.json'), "utf8");
    config = JSON.parse(config);
    const TARGET_PATH = path.join(config.android.platform.buildPath, "jsb-default/frameworks/runtime-src/proj.android-studio");
    let batInfo = fs.readFileSync('./creator_sources/batInfo.txt', "utf8");
    batInfo = batInfo.replace("CocosCreatorPath", config.android.platform.creatorPath);
    batInfo = batInfo.replace("projectPath", config.android.platform.projectPath);
    let batcom = fs.readFileSync('./creator_sources/batcom.txt', "utf8");
    batcom = batcom.replace("CocosCreatorPath", config.android.platform.creatorPath);
    batcom = batcom.replace("projectPath", config.android.platform.projectPath);
    try {
        fs.statSync(path.join(TARGET_PATH, "app/src/org/cocos2dx/javascript/WDNativePlatform.java"));
        //如果可以执行到这里那么就表示存在了
        console.log('项目已存在');
        fs.writeFile('./script/build.bat', batcom, "utf8", (err) => {
            if (err) throw err;
        })
    } catch (e) {
        batInfo = batInfo.replace("targetPath", TARGET_PATH);
        if (config.android.csjad.enable == true) {
            batInfo = batInfo.replace("adpath", "chuanshanjia");
        }else if (config.android.oppoad.enable == true) {
            batInfo = batInfo.replace("adpath", "oppo");
        }
        else if (config.android.vivoad.enable == true) {
            batInfo = batInfo.replace("adpath", "vivo");
        }
        else {
            batInfo = batInfo.replace("adpath", "huoxing");
        }

        fs.writeFile('./script/build.bat', batInfo, "utf8", (err) => {
            if (err) throw err;
        })
    }
    let build = fs.readFileSync('./creator_sources/builder.json', "utf8");
    build = build.replace("build_Path", config.android.platform.buildPath);
    build = build.replace(/com.wd.game/g, config.android.platform.packageName);
    build = build.replace("android-29", config.android.platform.apiLevel);
    build = build.replace("wdgame", config.android.platform.appName);
    if(config.android.platform.portrait==false){
        build = build.replace("\"landscapeLeft\": false,", "\"landscapeLeft\": true,");
        build = build.replace("\"landscapeRight\": false,", "\"landscapeRight\": true,");
        build = build.replace("\"portrait\": true,", "\"portrait\": false,");
        build = build.replace("\"upsideDown\": true,", "\"upsideDown\": false,");
    }
    
    fs.writeFileSync(path.join(config.android.platform.projectPath,'local/builder.json'), build, "utf8");
};


module.exports = Global;

